class Solution {
public:
    int searchInsert(vector<int>& nums, int target)
    {
        int lhs = 0, rhs = nums.size() - 1;
        int mid = lhs + (rhs - lhs + 1) / 2;

        while (lhs < rhs)
        {
            if (nums[mid] <= target) lhs = mid;
            else rhs = mid - 1;

            mid = lhs + (rhs - lhs + 1) / 2;
        }

        if (nums[lhs] == target) return lhs;
        else if (nums[lhs] < target) return lhs + 1;
        else return lhs;
    }
};